/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.like.example;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;

import com.like.LikeButton;
import com.like.LogUtil;
import com.like.OnAnimationEndListener;
import com.like.OnLikeListener;

/**
 * MainAbility
 */
public class MainAbility extends Ability implements OnLikeListener, OnAnimationEndListener {
    private static final String TAG = "MainAbility";
    private ToastDialog toastDialog;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        toastDialog = new ToastDialog(getAbilityPackageContext());

        LikeButton starButton = (LikeButton) findComponentById(ResourceTable.Id_star_button);
        starButton.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked(LikeButton likeButton) {
                displayToast("Like!");
            }

            @Override
            public void unLiked(LikeButton likeButton) {
                displayToast("Disliked!");
            }
        });

        starButton.setOnAnimationEndListener(new OnAnimationEndListener() {
            @Override
            public void onAnimationEnd(LikeButton likeButton) {
            }
        });

        LikeButton heartButton = (LikeButton) findComponentById(ResourceTable.Id_heart_button);
        heartButton.setOnLikeListener(this);
        heartButton.setOnAnimationEndListener(this);

        LikeButton thumbButton = (LikeButton) findComponentById(ResourceTable.Id_thumb_button);
        thumbButton.setOnLikeListener(this);
        thumbButton.setOnAnimationEndListener(this);

        LikeButton vectorButton = (LikeButton) findComponentById(ResourceTable.Id_vector);
        vectorButton.setOnLikeListener(this);
        vectorButton.setOnAnimationEndListener(this);

        LikeButton likeButton = (LikeButton) findComponentById(ResourceTable.Id_like_button);
        likeButton.setIconSizePx(300);
        likeButton.setLikeDrawableRes(ResourceTable.Graphic_ic_android_green_500_24vp);
        likeButton.setUnlikeDrawableRes(ResourceTable.Graphic_ic_android_grey_500_24vp);
        likeButton.setExplodingDotColorsInt(Color.getIntColor("#344955"),
                Color.getIntColor("#F9AA33"));
        likeButton.setLiked(true);
        likeButton.setCircleStartColorInt(Color.RED.getValue());
        likeButton.setCircleSEndColorInt(Color.BLUE.getValue());
    }

    @Override
    public void liked(LikeButton likeButton) {
        LogUtil.info(TAG, "Liked!");
    }

    @Override
    public void unLiked(LikeButton likeButton) {
        LogUtil.info(TAG, "Disliked!");
    }

    @Override
    public void onAnimationEnd(LikeButton likeButton) {
        LogUtil.info(TAG, "Animation End for %s" + likeButton);
    }

    private void displayToast(String message) {
        if (toastDialog.isShowing()) {
            toastDialog.cancel();
            toastDialog = new ToastDialog(this);
        }
        toastDialog.setText(message);
        toastDialog.setDuration(500);
        toastDialog.show();
    }
}
